const swaggerJSDoc = require('swagger-jsdoc')
const swaggerUi = require('swagger-ui-express')
const {swaggerConfig} = require('../config')
const swaggerDocument = require('./swagger.json')

module.exports = function setSwagger(app) {
  const options = {
      definition: {
          openapi: swaggerConfig.openapi,
          info: {
              title: swaggerConfig.title,
              version: swaggerConfig.version,
              description: swaggerConfig.description
          }
      },
      apis: swaggerConfig.apis
  }

  const swaggerSpec = swaggerJSDoc(options)

  app.get('/api-docs.json', (req, res) => {
      res.setHeader('Content-Type', 'application/json')
      res.send(swaggerSpec)
  })

  app.use(swaggerConfig.routerPath, swaggerUi.serve, swaggerUi.setup(swaggerDocument))
}